package com.example.photography.adapter.home_recommed;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.Activity.HotProduct_List;
import com.example.photography.Activity.My_Activity;
import com.example.photography.Activity.Product_Content;
import com.example.photography.R;
import com.example.photography.bean.CourseBean;
import com.example.photography.bean.Home_RecommendBean;
import com.example.photography.util.Util;
import com.google.android.material.snackbar.Snackbar;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.util.BannerUtils;

import java.util.List;

public class RecommedAdapter extends RecyclerView.Adapter {
    /**
     * 广告条幅
     */
    public static final int Ban = 0;

    /**
     * 榜单
     */
    public static final int Hot_List = 1;

    /**
     * 作品
     */
    public static final int Pro_List = 2;
    //类型
    private int currentType = 0;

    private Context context;
    private Home_RecommendBean homeRecommendBean;
    /**
     * 初始化布局
     */
    private final LayoutInflater layoutInflater;

    public RecommedAdapter(Context context, Home_RecommendBean homeRecommendBean) {
        this.context = context;
        this.homeRecommendBean = homeRecommendBean;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case Ban:
                currentType = Ban;
                break;
            case Hot_List:
                currentType = Hot_List;
                break;
            case Pro_List:
                currentType = Pro_List;
                break;
        }
        return currentType;
    }

    /**
     * 相当于getView 创建viewHolder部分代码
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Ban) {//广告轮转
            return new BanrViewHolder(context, layoutInflater.inflate(R.layout.item_banner, null));
        } else if (viewType == Hot_List) {//榜单
            return new Hot_ListViewHolder(context, layoutInflater.inflate(R.layout.item_hotlist, null));
        } else if (viewType == Pro_List) {
            return new Pro_ListViewHolder(context, layoutInflater.inflate(R.layout.item_listview, null));
        }
        return null;
    }

    /***
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == Ban) {
            BanrViewHolder banrViewHolder = (BanrViewHolder) holder;
            banrViewHolder.setData(homeRecommendBean.getBanner_list());
        } else if (getItemViewType(position) == Hot_List) {
            Hot_ListViewHolder hot_listViewHolder = (Hot_ListViewHolder) holder;
            hot_listViewHolder.setData(homeRecommendBean.getHot_product());
        } else if (getItemViewType(position) == Pro_List) {
            Pro_ListViewHolder pro_listViewHolder = (Pro_ListViewHolder) holder;
            pro_listViewHolder.setData(homeRecommendBean.getProduct_list());
        }
    }

    /**
     * 总共有多少个item类型
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return 3;
    }

    class BanrViewHolder extends RecyclerView.ViewHolder {
        private Context context;
        private Banner banner;

        public BanrViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            this.banner = view.findViewById(R.id.banner);
        }

        public void setData(List<Home_RecommendBean.BannerListBean> banner_list) {
            //设置Banner的数据
            /**
             * 获取轮转图片数据
             */
            banner.setAdapter(new BannerImageAdapter<Home_RecommendBean.BannerListBean>(banner_list) {
                @Override
                public void onBindView(BannerImageHolder holder, Home_RecommendBean.BannerListBean image, int position, int size) {
                    //图片加载自己实现
                    Glide.with(holder.itemView)
                            .load(image.getImage_url())
                            .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                            .into(holder.imageView);

                }
            });
            // TODO: 2021/5/4 点击事件有问题
            banner.setIndicator(new RectangleIndicator(context))//设置指示器
                    .setIndicatorSelectedWidth((int) BannerUtils.dp2px(12))
//                        .addBannerLifecycleObserver(this)//添加生命周期观察者(调用banner的addBannerLifecycleObserver()方法，让banner自己控制)
                    .setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(Object data, int position) {
                            Snackbar.make(banner, "推荐:"+((Home_RecommendBean.BannerListBean) data).getTitle(), Snackbar.LENGTH_SHORT).show();
                        }
                    });
        }
    }

    class Hot_ListViewHolder extends RecyclerView.ViewHolder {
        private Context context;
        private GridView gv_product;

        public Hot_ListViewHolder(Context context, View view) {
            super(view);
            this.context = context;
            this.gv_product = view.findViewById(R.id.gv_product);
        }

        public void setData(List<Home_RecommendBean.HotProductBean> hot_product) {
            Hot_ProductAdapter hot_productAdapter = new Hot_ProductAdapter(hot_product, context) {
                @Override
                void toproduct_detail(String product_id) {
                    Intent intent = new Intent(context, HotProduct_List.class);
                    context.startActivity(intent);
//                    Util.showToast(mContext,"product_id:"+product_id);
                }
            };
            gv_product.setAdapter(hot_productAdapter);
        }
    }
    class Pro_ListViewHolder extends RecyclerView.ViewHolder{
        private Context context;
        private ListView lv_product;
        Pro_ListViewHolder(Context context,View view) {
            super(view);
            this.context = context;
            this.lv_product = view.findViewById(R.id.lv_product);
        }
        public void setData(List<Home_RecommendBean.ProductListBean> product_list) {
            Item_productAdapter itemProductAdapter = new Item_productAdapter(product_list, context) {
                @Override
                public void tofocus(String id_unique) {
                    Util.showToast(mContext,"关注了该用户:"+id_unique);
                }

                @Override
                protected void toproduct_detail(String product_id) {
                    Util.showToast(mContext,"点击查看该作品:"+product_id);
                    Intent intent = new Intent(context, Product_Content.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("product_id",product_id);
                    intent.putExtra("data",bundle);
                    context.startActivity(intent);
                }

                @Override
                protected void to_User_home(String id_unique) {
                    Intent intent = new Intent(mContext, My_Activity.class);
                    context.startActivity(intent);
                }
            };
            lv_product.setAdapter(itemProductAdapter);

        }
    }


}
